#!/bin/bash

# 定义可配置参数
MODEL_NAME="qwen2.5vl-7b-tp4"
TOKENIZER_PATH="/home/username/Qwen2.5-VL-7B-Instruct-tp4/Qwen2.5-VL-7B-Instruct_llm_28layer_2048_4096_tp4_int8/tokenizer"
SERVER_PORT=9901


MAX_INPUT_LEN=512
MAX_OUTPUT_LEN=1024

MAX_SEQLEN=4096

PER_REQ_NUM_PROMPTS=5
INPUT_LENS=(128 256 512)
OUTPUT_LENS=(256)
MAX_CONCURRENCIES=(1 4)

IMAGE_SIZE="[1280,720]"


# 使用本地日期初始化日志目录
CURRENT_DATE=$(date +"%Y%m%d%H%M%S")
LOG_DIR="./benchmark_logs_${CURRENT_DATE}"
LOG_FILE="${LOG_DIR}/benchmark.log"

# 创建日志目录和文件
mkdir -p "$LOG_DIR" || { echo "无法创建日志目录 $LOG_DIR"; exit 1; }
> "$LOG_FILE" || { echo "无法清空日志文件 $LOG_FILE"; exit 1; }

# 定义带时间戳的日志函数
log() {
    local message="$1"
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "$LOG_FILE"
}

# 记录开始时间
start_time=$(date +%s)
log "=== Benchmark Started ==="
log "System Constraints:"
log "  MAX_INPUT_LEN         : $MAX_INPUT_LEN "
log "  MAX_OUTPUT_LEN        : $MAX_OUTPUT_LEN "
log "Testing Combinations:"
log "  Concurrency Levels : ${MAX_CONCURRENCIES[*]}"
log "  Output Lengths     : ${OUTPUT_LENS[*]}"
log "----------------------------------------"

# 遍历所有参数组合
for output_len in "${OUTPUT_LENS[@]}"; do
    for concurrency in "${MAX_CONCURRENCIES[@]}"; do
        # 跳过无效配置
        if (( output_len > MAX_OUTPUT_LEN )); then
            log "[SKIP] concurrency=$concurrency, output=$output_len 大于 MAX_INPUT_LEN=$MAX_OUTPUT_LEN"
            continue
        fi

        # 执行有效测试组合
        log "---- Testing concurrency=$concurrency output=$output_len) ----"
        for input_len in "${INPUT_LENS[@]}"; do
            # 跳过无效配置
            if (( input_len > MAX_INPUT_LEN )); then
                log "[SKIP] concurrency=$concurrency, input_len=$input_len 大于 MAX_INPUT_LEN=$MAX_INPUT_LEN"
                continue
            fi
            log "[START] concurrency=$concurrency, input=$input_len, output=$output_len"

            # 执行基准测试（带时间戳的详细日志）
            timestamp=$(date +"%Y%m%d_%H%M%S")
            result_file="${LOG_DIR}/${timestamp}_c${concurrency}_i${input_len}o${output_len}.json"

            start_test=$(date +%s)
            if ! python3 benchmark_serving.py \
                --host 127.0.0.1 \
                --port "$SERVER_PORT" \
                --model "$MODEL_NAME" \
                --dataset-name "random_vision" \
                --endpoint "/v1/chat/completions" \
                --backend "openai-chat" \
                --tokenizer "$TOKENIZER_PATH" \
                --num-prompts $(( PER_REQ_NUM_PROMPTS * concurrency )) \
                --random-input-len $input_len \
                --random-image-size "$IMAGE_SIZE" \
                --ignore-eos \
                --random-output-len $output_len \
                --max-concurrency $concurrency \
                --save-result \
                --result-filename "$result_file" 2>&1 |
                awk '{print strftime("[%Y-%m-%d %H:%M:%S]")" [PYTHON] "$0}' >> "$LOG_FILE"
            then
                log "[ERROR] 测试失败！concurrency=$concurrency,input=$input_len,output=$output_len"
                continue
            fi
            duration=$(( $(date +%s) - start_test ))

            log "[END] 耗时${duration}s | 资源使用：$(( concurrency * (input_len + output_len) ))/${MAX_SEQLEN}"
        done
    done
done

# 生成总结报告
total_duration=$(( $(date +%s) - start_time ))
log "============================================="
log "基准测试完成"
log "总耗时: $(( total_duration / 3600 ))小时 $(( (total_duration % 3600) / 60 ))分钟"
log "日志目录: $LOG_DIR"
log "关键参数:"
log "  MAX_INPUT_LEN     = $MAX_INPUT_LEN"
log "  MAX_OUTPUT_LEN    = $MAX_OUTPUT_LEN  "
log "============================================="